module Mix.Views {
    /**
     * Loading
     * 自定义读取资源界面
     */
    export class Loading extends Mix.Views.BaseViews{
        public loadingBar:eui.ProgressBar;
        public loadingLabel:eui.Label;
        
        constructor() {
            super();
        }
        
        public initCreationComplete(){
            super.initCreationComplete();
        }

        /**
         * loadGroup
         * 读取资源
         */
        public loadGroup(GroupName:string) {
            // this.stage.addChildAt(this,0);
            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.addEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            RES.loadGroup(GroupName);
        }
        
        /**
         * 资源组加载出错
         *  The resource group loading failed
         */
        private onItemLoadError(event:RES.ResourceEvent):void {
            console.warn("Url:" + event.resItem.url + " has failed to load");
        }
        
        /**
         * 资源组加载出错
         * Resource group loading failed
         */
        private onResourceLoadError(event:RES.ResourceEvent):void {
            //TODO
            console.warn("Group:" + event.groupName + " has failed to load");
            //忽略加载失败的项目
            //ignore loading failed projects
            this.onResourceLoadComplete(event);
        }
    
        /**
         * preload资源组加载进度
         * loading process of preload resource
         */
        private onResourceProgress(event:RES.ResourceEvent):void {
            this.setLoadProgress(event.itemsLoaded, event.itemsTotal);
        }
        
        /**
         * preload资源组加载完成
         * preload resource group is loaded
         */
        private onResourceLoadComplete(event:RES.ResourceEvent):void {
            RES.removeEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
            RES.removeEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceProgress, this);
            RES.removeEventListener(RES.ResourceEvent.ITEM_LOAD_ERROR, this.onItemLoadError, this);
            // this.stage.removeChild(this);
            this.loadGroupComplete();
        }
        
        /**
        * setLoadProgress
        * 设置loading进度条
        * @param current 当前资源值 
        * @param total 资源最大值 
        */
        public setLoadProgress(current:number, total:number) {
            
        }
        
        /**
         * loadGroupComplete
         */
        public loadGroupComplete() {
            
        }
    }
}